#!/usr/bin/env bash
#-------------------------------------------------------
#	FileName	: test.sh
#	Author		：hpy
#	Date		：2020年10月09日
#	Description	：
#-------------------------------------------------------

# 方向键检测
# 检测到方向键打印 up down left right ，否则默认检测到 enter
# 参数1：2次打拍的按键输入
# 参数2：1次打拍的按键输入
# 参数3：按键输入
function check_key(){
    inputs[0]=$1
    inputs[1]=$2
    inputs[2]=$3
    if [[ ${inputs[1]} == "[" ]];then
        if [[ ${inputs[2]} == "A" ]];then
            echo "up"
        elif [[ ${inputs[2]} == "B" ]] ;then
            echo "down"
        elif [[ ${inputs[2]} == "C" ]] ;then
            echo "right"
        elif [[ ${inputs[2]} == "D" ]] ;then
            echo "left"
        fi
    else 
        echo "enter"
    fi
}




while((1))
do
    read -sn1 key
    in_buf[0]=${in_buf[1]}
    in_buf[1]=${in_buf[2]}
    in_buf[2]=$key
    key=$(check_key ${in_buf[0]} ${in_buf[1]} ${in_buf[2]})

    if [[ $key == "up" ]];then
        echo up
    elif [[ $key == "down" ]];then
        echo down 
    elif [[ $key == "left" ]];then
        echo left 
    elif [[ $key == "right" ]];then
        echo right
    elif [[ $key != "null" ]];then
        echo $key
    fi 
done


